package com.woniu.controller;

import com.woniu.entity.RbacRole;
import com.woniu.entity.ResponseEntity;
import com.woniu.service.RbacRoleService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rain
 * @Description :
 * @date 2022/09/02 15:53
 */
@RestController
@RequestMapping("/rbacRole")
@CrossOrigin
public class RbacRoleController {
    @Autowired
    RbacRoleService rbacRoleService;

    @PostMapping("/{pageNum}/{pageSize}")
    public ResponseEntity findAll(@PathVariable int pageNum,
                                  @PathVariable int pageSize,
                                  @RequestBody RbacRole role) {
        return rbacRoleService.findAll(pageNum, pageSize,role);
    }

    @GetMapping("/{id}")
    public ResponseEntity findOne(@PathVariable("id") int id) {
        return rbacRoleService.findOne(id);
    }

    @PostMapping("/")
    public ResponseEntity addOne(@RequestBody RbacRole role) {
        return rbacRoleService.addOne(role);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity deleteOne(@PathVariable("id") int id) {
        return rbacRoleService.deleteOne(id);
    }

    @PutMapping("/")
    public ResponseEntity updateOne(@RequestBody RbacRole role) {
        return rbacRoleService.updateOne(role);
    }

    @PutMapping("/{id}/{status}")
    public ResponseEntity updateRoleStatus(@PathVariable Integer id,
                                           @PathVariable String status) {
        return rbacRoleService.updateRoleStatus(id, status);
    }

    /**
     * 通过用户ID查找权限ID,再通过权限ID查找所拥有的菜单
     * @param id 角色ID
     * @return ResponseEntity
     */
    @GetMapping("/findCheckedID/{id}")
    public ResponseEntity findCheckedID(@PathVariable Integer id){
        return rbacRoleService.findCheckedID(id);
    }

    @DeleteMapping("/{roleID}/{menuID}")
    public ResponseEntity removeMenuByRoleID(@PathVariable Integer roleID,
                                             @PathVariable Integer menuID){
        return rbacRoleService.removeMenuByRoleID(roleID,menuID);
    }

    @PostMapping("addMenuByRoleID/{roleID}/{menuID}")
    public ResponseEntity addMenuByRoleID(@PathVariable Integer roleID,
                                             @PathVariable Integer menuID){
        return rbacRoleService.addMenuByRoleID(roleID,menuID);
    }


    @PostMapping("roleMenuSave/{roleID}")
    public ResponseEntity roleMenuSave(@PathVariable Integer roleID,
                                       @RequestBody List<Integer> menuIDs){
        return rbacRoleService.roleMenuSave(roleID,menuIDs);
    }
}
